import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/common/Passwords.dart';
import 'package:ichinesereader/models/forms/forms.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:reactive_forms/reactive_forms.dart';

import '../../themes/appTheme.dart';

class ChangeUserPasswordReactiveDialog extends StatelessWidget {
  ChangeUserPasswordReactiveDialog({
    Key? key,
    required this.isTeacherOrSchooladmin,
  }) : super(key: key);
  final bool isTeacherOrSchooladmin;

  @override
  Widget build(BuildContext context) {
    UserController _usercontroller = Get.find();
    ChangePasswordFormModel formModel = ChangePasswordFormModel();
    var form = formModel.form;
    ChangePasswordFormTeacherModel formTeacherModel =
        ChangePasswordFormTeacherModel();
    var formTeacher = formTeacherModel.form;
    String teacherPasswordMessage =
        '''*Passwords contain at least 12 characters, 1 number, 1 uppercase letter, and 1 special character.''';

    return AlertDialog(
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            'Reset Password'.tr,
            style: TextStyle(
                fontSize: 30.0,
                decoration: TextDecoration.underline,
                decorationStyle: TextDecorationStyle.dotted),
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Icon(Icons.cancel,
                color: Get.isDarkMode ? Colors.white : Colors.blueGrey),
          ),
        ],
      ),
      titleTextStyle: TextStyle(
          letterSpacing: 1,
          color: Get.isDarkMode ? Get.theme.primaryColorDark : Colors.blueGrey,
          fontSize: 21),
      backgroundColor:
          Get.isDarkMode ? AppTheme.colors.darkTheme2 : Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(6.0),
        ),
      ),
      content: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          width: 450,
          height: 300,
          child: ReactiveForm(
            formGroup: isTeacherOrSchooladmin ? formTeacher : form,
            child: Column(children: [
              Expanded(
                  child: ReactiveTextField(
                formControlName: 'currentpassword',
                obscureText: true,
                decoration: InputDecoration(
                  labelText: 'Current Password'.tr,
                  labelStyle: TextStyle(
                    color: Get.isDarkMode
                        ? Get.theme.primaryColor
                        : Colors.grey[400],
                  ),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(3.0),
                  ),
                ),
                validationMessages: {
                  ValidationMessage.minLength: (_) =>
                      'Password must be at least 6 characters in length'.tr
                },
              )),
              Expanded(
                child: ReactiveTextField(
                  formControlName: 'newpassword',
                  obscureText: true,
                  decoration: InputDecoration(
                    labelText: 'New Password'.tr,
                    labelStyle: TextStyle(
                      color: Get.isDarkMode
                          ? Get.theme.primaryColor
                          : Colors.grey[400],
                    ),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(3.0),
                    ),
                  ),
                  validationMessages: {
                    ValidationMessage.pattern: (_) =>
                        'Password pattern not right'.tr
                  },
                ),
              ),
              Visibility(
                visible: isTeacherOrSchooladmin,
                child: Text(
                  teacherPasswordMessage,
                  style: TextStyle(
                      fontSize: 12.0,
                      color: Get.isDarkMode
                          ? Colors.white
                          : AppTheme.colors.pinkBtn),
                ),
              ),
              SizedBox(
                height: 21,
              ),
              ReactiveFormConsumer(
                builder: (context, form, child) {
                  return Container(
                    width: 210.0,
                    height: 45.0,
                    child: MaterialButton(
                      color: Get.isDarkMode
                          ? (Colors.white)
                          : Theme.of(context).primaryColor,
                      textColor: Get.isDarkMode
                          ? (Colors.black)
                          : Theme.of(context).primaryColorLight,
                      onPressed: () {
                        if (form.valid) {
                          Passwords passwords = new Passwords(
                              newPassword: form.control('newpassword').value,
                              password: form.control('currentpassword').value);
                          _usercontroller
                              .changeUserPassword(passwords)
                              .then((msgStatus) {
                            if (msgStatus.status == 1000) {
                              showSnackMsg('Password reset successfully');
                              // Get.snackbar(
                              //   'Message'.tr,
                              //   'Password reset successfully'.tr,
                              //   snackPosition: SnackPosition.BOTTOM,
                              //   backgroundColor:
                              //       Theme.of(context).primaryColor,
                              //   colorText:
                              //       Theme.of(context).primaryColorLight,
                              // );
                              Future.delayed(Duration(milliseconds: 1000), (() {
                                Navigator.of(context).pop();
                              }));
                            } else {
                              showSnackMsg('Failed to reset password');
                              // Get.snackbar(
                              //   'Message'.tr,
                              //   'Failed to reset password'.tr,
                              //   snackPosition: SnackPosition.BOTTOM,
                              //   backgroundColor:
                              //       Theme.of(context).primaryColor,
                              //   colorText:
                              //       Theme.of(context).primaryColorLight,
                              // );
                            }
                          });
                        } else {
                          form.markAllAsTouched();
                        }
                      },
                      child: Text(
                        'Submit'.tr,
                        style: TextStyle(fontSize: 18.0),
                      ),
                    ),
                  );
                },
              ),
            ]),
          ),
        ),
      ),
    );
  }
}
